<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="RoleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row}">
                  <el-button size="small" type="success" @click="allotPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :current-page="params.page"
                :page-size="params.pagesize"
                :total="total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="120px"
              style="margin-top: 50px"
              :model="companyInfo"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 编辑模态框 -->
      <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
        <el-form v-if="showDialog" ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="{label:'name'}"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRole, assignPerm, delRole, editRole, getCompanyInfo, getRoleDetail, getRoleList } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { arr2tree } from '@/utils'
export default {
  data() {
    return {
      companyInfo: {}, // 公司信息
      params: {
        // 分页参数
        page: 1,
        pagesize: 5
      },
      RoleList: [], // 角色列表
      total: 0, // 角色列表总条数
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // 权限数据
      permData: [],
      selectCheck: [],
      showPermDialog: false,
      roleId: ''
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.getPermissionList()
  },
  methods: {
    // 分配權限
    async allotPerm(id) {
      this.roleId = id
      const res = await getRoleDetail(id)
      this.selectCheck = res.permIds
      this.showPermDialog = true
      console.log(res)
    },
    // 权限模态框ok
    async btnPermOK() {
      await assignPerm({
        id: this.roleId,
        permIds: this.selectCheck
      })
      this.$message.success('操作成功')
      this.showPermDialog = false
    },
    // 权限模态框关闭
    btnPermCancel() {
      this.showPermDialog = false
    },
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermissionList()
      this.permData = arr2tree(res, '0')
    },
    // 获取公司信息
    async getCompanyInfo() {
      const res = await getCompanyInfo(
        this.$store.state.user.userInfo.companyId
      )
      this.companyInfo = res
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleList(this.params)
      this.RoleList = res.rows
      this.total = res.total
    },
    // 切换分页
    currentChange(val) {
      this.params.page = val
      this.getRoleList()
    },
    // 新增角色
    addRole() {
      this.showDialog = true
    },
    // 删除角色
    async delRole(id) {
      try {
        await this.$confirm('真的要删除嘛？')
        await delRole(id)
        this.$message.success('操作成功')
        // 如果此页非第一页 且只剩一条数据了 删除后就返回前一页
        if (this.RoleList.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑角色
    async editRole(id) {
      const res = await getRoleDetail(id)
      // 回显数据
      this.roleForm = res
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清除校验
      this.$refs.roleForm.resetFields()
    },
    async btnOK() {
      // 校验
      await this.$refs.roleForm.validate()

      if (this.roleForm.id) {
        // 编辑
        await editRole(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
      }
      this.showDialog = false
      this.$message.success('操作成功')
      // 去最后一页
      this.params.page = Math.ceil(this.total / this.params.pagesize)
      this.getRoleList()
    }
  }
}
</script>

<style>
</style>
